//
//  SmallDecisionsSlider.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/25.
//

#import "SmallDecisionsSlider.h"
@interface SmallDecisionsSlider()

@property (nonatomic, copy) NSString *(^minValueFormat)(UISlider *slider);
@property (nonatomic, copy) NSString *(^maxValueFormat)(UISlider *slider);

@end

@implementation SmallDecisionsSlider

@synthesize value = _value;
@synthesize minimumValue = _minimumValue;
@synthesize maximumValue = _maximumValue;
- (instancetype)initWithMinValueFormat:(NSString *(^ _Nullable)(UISlider *slider))minValueFormat
                        maxValueFormat:(NSString *(^ _Nullable)(UISlider *slider))maxValueFormat{
    self = [super init];
    if (self) {
        self.minValueFormat = minValueFormat;
        self.maxValueFormat = maxValueFormat;
        [self createSmallDecisionsSliderSubviews];
    }
    return self;
}

- (void)createSmallDecisionsSliderSubviews {
    [self slider];
    [self titleLabel];
    [self minLabel];
    [self maxLabel];
}

- (UISlider *)slider {
    if(!_slider) {
        _slider = [[UISlider alloc] init];
        _slider.minimumTrackTintColor = SmallDecisionsThemeFontColor;
        _slider.maximumTrackTintColor = [UIColor colorWithRGB:0xeeeeee];
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateNormal];
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateFocused];
        [_slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
        _slider.continuous = YES;
        [self addSubview:_slider];
        [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.left.mas_equalTo(70);
            make.height.mas_equalTo(20);
            make.right.mas_equalTo(-10);
        }];
    }

    return _slider;
}

- (UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SmallDecisionsThemeFontColor;
        _titleLabel.font = SmallDecisionsSetFontSize(14);
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.slider.mas_centerY);
            make.left.mas_equalTo(0);
        }];
    }

    return _titleLabel;
}

- (UILabel *)minLabel {
    if(!_minLabel) {
        _minLabel = [[UILabel alloc] init];
        _minLabel.textColor = SmallDecisionsThemeFontColor;
        _minLabel.font = SmallDecisionsSetFontSize(12);
        [self addSubview:_minLabel];
        [_minLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.slider.mas_bottom).mas_offset(5);
            make.left.mas_equalTo(self.slider.mas_left);
            make.height.mas_equalTo(15);
        }];
    }

    return _minLabel;
}

- (UILabel *)maxLabel {
    if(!_maxLabel) {
        _maxLabel = [[UILabel alloc] init];
        _maxLabel.textColor = SmallDecisionsThemeFontColor;
        _maxLabel.font = SmallDecisionsSetFontSize(12);
        [self addSubview:_maxLabel];
        [_maxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.slider.mas_bottom).mas_offset(5);
            make.right.mas_equalTo(self.slider.mas_right);
            make.bottom.mas_equalTo(-10);
            make.height.mas_equalTo(15);
        }];
    }

    return _maxLabel;
}

- (void)sliderValueChange:(UISlider *)slider {
    if (self.sliderValueChange) {
        self.sliderValueChange(self);
    }
    if (self.minValueFormat) {
        self.minLabel.text = self.minValueFormat(self.slider);
    }
    if (self.maxValueFormat) {
        self.maxLabel.text = self.maxValueFormat(self.slider);
    }
}

- (void)setMinimumValue:(float)minimumValue {
    _minimumValue = minimumValue;
    self.slider.minimumValue = minimumValue;
    [self sliderValueChange:self.slider];
}

- (void)setMaximumValue:(float)maximumValue {
    _maximumValue = maximumValue;
    self.slider.maximumValue = maximumValue;
    [self sliderValueChange:self.slider];
}

- (void)setValue:(float)value {
    _value = value;
    self.slider.value = value;
    [self sliderValueChange:self.slider];
}

- (float)value {
    return self.slider.value;
}

- (float)minimumValue {
    return self.slider.minimumValue;
}

- (float)maximumValue {
    return self.slider.maximumValue;
}


@end
